package com.twoandone.tms.realtime.utils;

import org.apache.commons.beanutils.BeanUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @packageName:com.twoandone.tms.realtime.utils
 * @ClassName:PhoenixUtil
 * @Description:
 * @author:
 * @date 2023/3/9 22:55
 **/
public class PhoenixUtil {

    public static void excuteSql(String sql) {
        Connection conn = null;
        PreparedStatement prep = null;
        try {
            conn = DruidDSUtil.getConnection();
            prep = conn.prepareStatement(sql);
            prep.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (prep != null) {
                try {
                    prep.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

    public static <T> List<T> queryList(String sql, Class<T> clz) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        ArrayList<T> list = new ArrayList<>();
        try {
            //获取与数据库的连接
            connection = DruidDSUtil.getConnection();
            //创建sql执行对象
            ps = connection.prepareStatement(sql);
            //执行sql
            rs = ps.executeQuery();
            //处理结果集
            while (rs.next()) {
                //创建对象用于封装结果集
                T obj = clz.newInstance();
                ResultSetMetaData metaData = rs.getMetaData();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    String columnName = metaData.getColumnName(i);
                    Object columnValue = rs.getObject(i);
                    BeanUtils.setProperty(obj, columnName, columnValue);
                }
                list.add(obj);
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭连接
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return list;
    }

}
